package org.example.clouddemo.controller.app;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.example.clouddemo.bean.common.entity.Order;
import org.example.clouddemo.bean.common.entity.Product;
import org.example.clouddemo.bean.common.param.CreateOrderParam;
import org.example.clouddemo.feign.ProductFeignClient;
import org.example.clouddemo.service.common.OrderService;
import org.example.clouddemo.service.common.UserService;
import org.example.common.annotation.IgnoreAuth;
import org.example.common.bean.LoginUser;
import org.example.common.exception.AppRunTimeException;
import org.example.common.response.ApiResult;
import org.example.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 订单接口
 * </p>
 *
 * @author hzg
 * @since 2025-06-08
 */

@Api(tags = "订单接口")
@ApiSupport(order = 2)
@RestController("appOrderController")
@RequestMapping("/api/app/order")
@Log4j2
public class OrderController {

    @Autowired
    public UserService userService;

    @Autowired
    public OrderService orderService;

    @Autowired
    public ProductFeignClient productFeignClient;

    @ApiOperation(value = "创建订单", notes = "返回结果是订单id")
    @ApiOperationSupport(order = 1)
    @PostMapping("createOrder")
    public ApiResult<Long> createOrder(@Validated @RequestBody CreateOrderParam param) {

        //TODO: 暂时把逻辑写在这里,演示用, 后续应该放在对应的service中



        // 创建订单逻辑
        // 这里可以添加库存检查、价格计算等逻辑
        Long orderId = orderService.createOrder(param);

        return ApiResult.success(orderId);
    }
    @ApiOperation(value = "获取订单详情", notes = "根据订单ID获取订单详情")
    @ApiOperationSupport(order = 2)
    @PostMapping("orderDetail/{page}/{pageSize}")
    public ApiResult getOrderDetail(@PathVariable("page") int page, @PathVariable("pageSize") int pageSize) {
        Page pageInfo = new Page<>();
        pageInfo.setCurrent(page);
        pageInfo.setSize(pageSize);
        // 调用 service 获取订单详情
        Page orderDetail = orderService.getOrderDetail(pageInfo);

        if (orderDetail == null) {
            return ApiResult.error("订单不存在");
        }

        return ApiResult.success(orderDetail);
    }

    @ApiOperation(value = "取消订单", notes = "根据订单ID取消订单")
    @ApiOperationSupport(order = 3)
    @PostMapping("cancelOrder/{orderId}")
    public ApiResult<Boolean> cancelOrder(@PathVariable("orderId") Long orderId) {
        // 获取当前登录用户信息
        LoginUser loginUser = SecurityUtils.getUserInfo();

        // 调用 service 层取消订单逻辑
        boolean result = orderService.cancelOrder(orderId, loginUser);

        if (result) {
            return ApiResult.success(true);
        } else {
            return ApiResult.error("取消订单失败");
        }
    }


    @ApiOperation(value = "测试feign调用", notes = "远程调用service-product的/api/app/product/queryOne的接口")
    @ApiOperationSupport(order = 99)
    @PostMapping("test")
    @IgnoreAuth
    public ApiResult<JSONObject> test(@Validated @RequestBody CreateOrderParam param) {

        //获取产品信息

        Product find = new Product();
        find.setId(param.getProductId());
        ApiResult<Product> productApiResult = productFeignClient.getProductById(find);

        if (!productApiResult.isSuccess()) {
            throw new AppRunTimeException("找不到产品");
        }

        Product product = productApiResult.data;
        log.info("getProductById:{}", product);

        Product product2 = productFeignClient.getProductById2(find);

        log.info("getProductById2:{}", product2);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("productId", param.getProductId());
        jsonObject.put("getProductById", productApiResult);
        jsonObject.put("getProductById2", product2);
        return ApiResult.success(jsonObject);
    }
}
